//
//  ZYEmotionToolbar.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/4.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit
//照中间的tag值
private let tagNum = 1000
//表情键盘类型的枚举
enum ZYEmotionToolbarType: Int {
    //默认
    case Default = 0
    //Emoji表情
    case Emoji = 1
    //浪小花
    case Lxl = 2
    
}

class ZYEmotionToolbar: UIView {
    
    //设置选中的状态
    var currentButton: UIButton?
    
    //声明一个闭包,告知哪个按钮被点击了
    var toolbarClosure:((type: ZYEmotionToolbarType) -> ())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //---------------7.5----------设置一个供外界调用的方法
    func setCurrentButtons(tag:Int) {
        
        //通过tag来获取button   ///---------------------7.5------
        let button = viewWithTag(tag + tagNum) as! UIButton
        
        /*
        1.将传入的button变为选中的状态
        2.将currentButton变为正常状态
        3.currentButton == 传入的button
        
        */
        if currentButton == button {
            return
        }
        button.selected = true
        currentButton?.selected = false
        currentButton = button
        
       // print("点击正确")
        
        
        
        //执行闭包
        //toolbarClosure?(type: ZYEmotionToolbarType(rawValue: button.tag)!)

    
    }
    
    
    
    //MARK: - 设置toolBarUI
    func setupUI() {
    
        //backgroundColor = RandomColor()
        
        let defaultBtn = addChildButtons("left", title: "默认", type: .Default)
        let EmojiBtn = addChildButtons("mid", title: "Emoji", type: .Emoji)
        let langtBtn = addChildButtons("right", title: "浪小浪", type: .Lxl)
        
        defaultBtn.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(EmojiBtn)
            
        }
        
        EmojiBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultBtn.snp_trailing)
            make.width.equalTo(langtBtn)
            
        }
        
        langtBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.top.bottom.equalTo(self)
            make.leading.equalTo(EmojiBtn.snp_trailing)
            
        }
        
        
        //---------------7.5----------设置首次按钮为选中状态
        defaultBtn.selected = true
        currentButton = defaultBtn
            
    }
    
    //MARK: - button按钮的监听方法
    func btnClick(button: UIButton) {
        
        //7.4 7:53
        /*
        1.将传入的button变为选中的状态
        2.将currentButton变为正常状态
        3.currentButton == 传入的button
        
        */
        if currentButton == button {
            return
        }
         button.selected = true
        currentButton?.selected = false
        currentButton = button
        
       // print("点击正确")
        
        
        
        //执行闭包
        toolbarClosure?(type: ZYEmotionToolbarType(rawValue: button.tag - tagNum)!)
        
    }
    
    //创建button的公共方法
    func addChildButtons(imagName:String, title:String, type: ZYEmotionToolbarType) -> UIButton {
        
        
        
        
        
        let button = UIButton()
        
        // 设置tag
        //把枚举值转换成Int类型的   7.5  加上tagNum
        button.tag = type.rawValue + tagNum
        
        //设置button的点击事件
        button.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // compose_emotion_table_left_normal
        //compose_emotion_table_left_selected
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imagName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imagName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        self.addSubview(button)
        
        return button
    }
   
    
}




























